package components.base
{
	import mx.containers.TitleWindow;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.validators.IValidator;
	import mx.validators.Validator;
	
	import spark.components.TextArea;
	import spark.components.TextInput;
	import spark.filters.DropShadowFilter;
	
	import common.constants.AppConstants;
	
	import components.effects.BaseEffects;
	
	import events.ObjectEvent;
	
	import framework.AppGlobal;
	import framework.LangMgr;
	import framework.PopUpWin;
	
	import interfaces.IDispose;
	import interfaces.IValidation;
	
	import org.spicefactory.parsley.core.context.Context;
	import org.spicefactory.parsley.view.Configure;
	
	public class BaseTitleWindow extends TitleWindow implements IDispose, IValidation
	{
		[Inject]
		public var context:Context;
		
		/**要验证的数据集合**/
		private var validators:Array = [];
		
		/**关联的对象**/
		[Bindable]
		public var item:Object;
		
		[Bindable]
		public var type:String;
		
		public function BaseTitleWindow()
		{
			super();
			Configure.view(this).execute();
			var ds:DropShadowFilter = new DropShadowFilter(4, 90, 0x646464, 0.5, 6, 6);
			filters = [ds];
			if(AppGlobal.effectOn)
				alpha = 0.01;
			showCloseButton = true;
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			addEventListener(AppConstants.GET_VALIDATION, getValidationHandler);
			addEventListener(CloseEvent.CLOSE, closeHandler);
		}
		
		/**
		 * 控件创建完成后调用初始化信息
		 */
		private function creationCompleteHandler(p_evt:FlexEvent):void
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			if(styleName == null)
				styleName = "titleWindow";
			if(AppGlobal.effectOn)
				BaseEffects.play(this, "show", width/2, height/2, 0.6, 1, 0, 1);
			else
				alpha = 1;
		}
		
		private function closeHandler(event:CloseEvent):void
		{
			close();
		}
		
		override public function set title(value:String):void
		{
			var str:String = LangMgr.getResourceByLocaleKey(value);
			super.title = str;
		}
		
		/**
		 * 国际化信息
		 */
		private var _locale:String;
		public function get locale():String
		{
			return _locale;
		}
		public function set locale(p_str:String):void
		{
			_locale = p_str;
			//加载语言包
			if(_locale)
				LangMgr.loadLangPkg(_locale);
		}
		
		/**
		 * 实现国际化
		 */
		override protected function resourcesChanged():void
		{
			super.resourcesChanged();
			if(_locale)
				title = _locale;
		}
		
		public function dispose():void
		{
			removeEventListener(CloseEvent.CLOSE, closeHandler);
			removeEventListener(AppConstants.GET_VALIDATION, getValidationHandler);
			if(context)
				context.viewManager.removeViewRoot(this);
			//清空文本
			for(var i:int = 0;i < validators.length;i++){
				var v:Object = validators[i];
				if(v.source is TextInput || v.source is TextArea)
					v.source.text = "";
			}
			validators = [];
		}
		
		public function pushValidator(p_validator:IValidator):void
		{
			var index:int = validators.indexOf(p_validator);
			if(index < 0)
				validators.push(p_validator);
		}
		
		private function getValidationHandler(p_evt:ObjectEvent):void
		{
			p_evt.stopPropagation();
			if(p_evt.data != null && p_evt.data is Function){
				p_evt.data(this);
			}
		}
		
		protected function close():void
		{
			if(AppGlobal.effectOn)
				BaseEffects.play(this, "hide", width/2, height/2, 1, 0.8, 1, 0, removeWin);
			else
				removeWin();
		}
		
		private function removeWin():void
		{
			PopUpWin.removePopUpWin(this);
		}
		
		public function validate():Array
		{
			// TODO Auto Generated method stub
			if(!validators || validators.length <= 0)return null;
			return Validator.validateAll(validators);
		}
		
		/**
		 * 扩展属性显示动画
		 * @param p_target
		 * @param p_xFrom
		 * @param p_xTo
		 * 
		 */		
		protected function showExPropEffect(p_targets:Array, p_xFrom:Number, p_xTo:Number):void
		{
			if(!AppGlobal.effectOn){
				for(var i:int = 0;i < p_targets.length;i++){
					var ui:UIComponent = p_targets[i] as UIComponent;
					if(ui)
						ui.x = p_xTo;
				}
			}else{
				BaseEffects.transform(p_targets, p_xFrom, p_xTo);
			}
		}
	}
}